//
//  HappyDatabase.m
//  HappyTrip
//
//  Created by GeminyXC on 15-4-6.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HappyDatabase.h"

@implementation HappyDatabase

+ (HappyDatabase *)shareInstance
{
    static HappyDatabase *dbHander = nil;
    if (dbHander == nil) {
        dbHander = [[HappyDatabase alloc] init];
    }
    return dbHander;
}

- (void)judgeResult:(int)result type:(NSString *)type
{
    if (result == SQLITE_OK) {
        NSLog(@"%@成功", type);
    }else{
        NSLog(@"%@失败, 失败编号: %d", type, result);
    }
}

- (void)openDB
{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *dbPath = [docPath stringByAppendingPathComponent:@"happy.db"];
    
    NSLog(@"%@", dbPath);
    
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    [self judgeResult:result type:@"打开数据库"];
}

- (void)closeDB
{
    int result = sqlite3_close(dbPoint);
    [self judgeResult:result type:@"关闭数据库"];
}

- (void)createTable
{
    NSString *sqlHappy = @"create table happyTable (theType text, theUrlId text primary key, theName text)";
    int result = sqlite3_exec(dbPoint, [sqlHappy UTF8String], NULL, NULL, NULL);
    [self judgeResult:result type:@"创建收藏表"];
}

- (void)insertHappyObject:(HappyObject *)happyObj
{
    NSString *insertSqlHappy = [NSString stringWithFormat:@"insert into happyTable values ('%@', '%@', '%@')", happyObj.theType, happyObj.theUrlId, happyObj.theName];
    int result = sqlite3_exec(dbPoint, [insertSqlHappy UTF8String], NULL, NULL, NULL);
    [self judgeResult:result type:@"插入收藏数据"];
}

- (void)deleteHappyObject:(HappyObject *)happyObj
{
    NSString *deleteSqlHappy = [NSString stringWithFormat:@"delete from happyTable where theUrlId = '%@'", happyObj.theUrlId];
    int result = sqlite3_exec(dbPoint, [deleteSqlHappy UTF8String], NULL, NULL, NULL);
    [self judgeResult:result type:@"删除收藏数据"];
}

- (NSMutableArray *)selectAll
{
    NSString *selectSqlHappy = @"select * from happyTable";
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare(dbPoint, [selectSqlHappy UTF8String], -1, &stmt, NULL);
    NSMutableArray *theHappyArr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            HappyObject *happyObject = [[HappyObject alloc] init];
            
            //theType
            const unsigned char *theTypeChar = sqlite3_column_text(stmt, 0);
            NSString *theType = [NSString stringWithUTF8String:(const char *)theTypeChar];
            happyObject.theType = theType;
          

            //theUrlId
            const unsigned char *theUrlIdChar = sqlite3_column_text(stmt, 1);
            NSString *theUrlId = [NSString stringWithUTF8String:(const char *)theUrlIdChar];
            happyObject.theUrlId = theUrlId;
            
            //theName
            const unsigned char *theNameChar = sqlite3_column_text(stmt, 2);
            NSString *theName = [NSString stringWithUTF8String:(const char *)theNameChar];
            happyObject.theName = theName;
            
            [theHappyArr addObject:happyObject];
            [happyObject release];
        }
    }
    sqlite3_finalize(stmt);
    return theHappyArr;
}

@end
